using System;
using System.Collections;

namespace Scenario.Modules.FormHelper
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class FormAttributeOperations : CollectionBase
	{
		//private ArrayList<Operations> operations;
		//ArrayList operations;
		public FormAttributeOperations()
		{
			//this.operations= new ArrayList();
			
		}

		public void Add(FormAttributeOperation o)
		{
			//this.operations.Add(o);
			InnerList.Add(o);
		}

		public int SearchDocIDForIN(string sDocID)
		{
			/* Ricerca la posizione di sDocID nella collezione di FAOperation
			 * e restitusce l'elemento dove resource e from sono vuoti*/
			foreach (FormAttributeOperation o in this.InnerList)
			{
				if (String.Compare(sDocID,o.DocID)==0)
				{
					if ((o.From==string.Empty)&& (o.Resource==string.Empty))
						return this.InnerList.IndexOf(o);
				}
			}
			return -1;
		}
		public int SearchDocIDForOUT(string sDocID)
		{
			/* Ricerca la posizione di sDocID nella collezione di FAOperation
			 * e restitusce l'elemento dove resource e To sono vuoti*/
			foreach (FormAttributeOperation o in this.InnerList)
			{
				if (String.Compare(sDocID,o.DocID)==0)
				{
					if ((o.To==string.Empty)&& (o.Resource==string.Empty))
						return this.InnerList.IndexOf(o);
				}
			}
			return -1;
		}
		public int SearchResource(string sRes)
		{
			
			foreach (FormAttributeOperation o in this.InnerList)
			{
				if (String.Compare(sRes,o.Resource)==0)
				{
					return this.InnerList.IndexOf(o);
				}
			}
			return -1;
		}
		public int SearchDocID(string sDocId)
		{
			
			foreach (FormAttributeOperation o in this.List)
			{
				if (String.Compare(sDocId,o.DocID)==0)
				{
					return this.InnerList.IndexOf(o);
				}
			}
			return -1;
		}

		public void SortByDocID()
		{
			this.Sort(SortFields.DOC_ID,true);
		}

		public void SortByResource()
		{
			this.Sort(SortFields.RESOURCE, true);
		}

		public enum SortFields 
		{
			DOC_ID,
			RESOURCE
		}

		private void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.DOC_ID:
					InnerList.Sort(new IDComparer());
					break;
				case SortFields.RESOURCE:
					InnerList.Sort(new ResourceComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class IDComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				FormAttributeOperation first = (FormAttributeOperation) x;
				FormAttributeOperation second = (FormAttributeOperation) y;

				return (string.Compare(first.DocID, second.DocID) );
			}
		}
		private sealed class ResourceComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				FormAttributeOperation first = (FormAttributeOperation) x;
				FormAttributeOperation second = (FormAttributeOperation) y;

				return (string.Compare(first.Resource, second.Resource) );
			}
		}
		public void setFrom(int index, string sFrom)
		{
			FormAttributeOperation x= (FormAttributeOperation)this.InnerList[index];
			x.From=sFrom;
		}
		public void setResource(int index, string sRes)
		{
			FormAttributeOperation x= (FormAttributeOperation)this.InnerList[index];
			x.Resource=sRes;
		}
		public void setTo(int index, string sTo)
		{
			FormAttributeOperation x= (FormAttributeOperation)this.InnerList[index];
			x.To=sTo;
		}
		public void setDocID(int index, string sDocID)
		{
			FormAttributeOperation x= (FormAttributeOperation)this.InnerList[index];
			x.DocID=sDocID;
		}
		public void setDocName(int index, string sDocName)
		{
			FormAttributeOperation x= (FormAttributeOperation)this.InnerList[index];
			x.DocName=sDocName;
		}


		public int IndexOf(FormAttributeOperation o)
		{
			return this.InnerList.IndexOf(o);
		}

	}
}
